﻿using Sunny.UI;
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
using CoreShop.Model.FromBody;
using CoreShop.Library;
using CoreShop.Model;
using System.Collections.Generic;
using CoreShop.Services.Services.Report;
using CoreShop.App.Units;
using System.Windows.Forms;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using CoreShop.Services;

namespace CoreShop.App
{
    public partial class fpgSysUserSum : UIPage
    {
        public int mPageIndex = 0;

        public fpgSysUserSum()
        {
            InitializeComponent();
            LoadComboxData();
        }

        private void LoadComboxData()
        {
            #region 加载支付方式
            this.userId.Items.Clear();
            List<sysuser> user = new sysuserServices().GetList().Where(p => p.deleted == false && p.state == 0).ToList();
            userId.DataSource = user;
            userId.DisplayMember = "userName";
            userId.ValueMember = "id";
            userId.Text = "";
            userId.SelectedValue = 0;
            #endregion
        }


        private void fpgSysUserSum_ReceiveParams(object sender, UIPageParamsArgs e)
        {
            mPageIndex = 1;
            var rptRequest = e.Value as PosReportRequest;
            if (rptRequest.type == (int)GlobalEnumVars.ReportRequest.Query)
            {
                LoadData(rptRequest);
            }
            else
            {
                ExportData();
            }
        }

        private void ExportData()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Export Excel File To";
            saveFileDialog.ShowDialog();
            string fileName = saveFileDialog.FileName;
            if (fileName == "") return;
            Dictionary<string, DataGridView> gridViews = new Dictionary<string, DataGridView>();
            gridViews.Add("收银员汇总", dgvSysUserPayList);
            if (ExportUtil.NPOIExportForDataGridview(gridViews, fileName))
            {
                MessageBox.Show("导出Excel成功！");
            }
        }


        /// <summary>
        ///销售统计
        /// </summary>
        private void LoadData(PosReportRequest posReport)
        {
            List<ReportUserClass> lstSource = new List<ReportUserClass>();
            List<sysuser> user = new sysuserServices().GetList().Where(p => p.deleted == false && p.state == 0).ToList();
            int a = userId.SelectedIndex;
            if (CommFunc.ConvertToBtInt(userId.SelectedValue) > 0)
            {
                user = user.Where(p => p.id == (int)userId.SelectedValue).ToList();
            }
            foreach (sysuser item in user)
            {
                ReportUserClass userclass = new ReportUserClass();
                userclass.name = item.userName;
                ReportUserMaster master = new corereportServices().GetReportUserMaster(item.id , posReport.starTime, posReport.endTime);
                if (master != null)
                {
                    userclass.sumAmount = master.sumAmount;
                    userclass.sumRealAmount = master.sumRealAmount;
                    userclass.sumTaskAmount = master.sumTaskAmount;
                    userclass.sumFeedAmount = master.sumFeedAmount;
                    userclass.sumReduceAmount = master.sumReduceAmount;
                    userclass.sumAllAmount = master.sumAllAmount;
                }
                ReportUserBalance balance = new corereportServices().GetReportUserBalance(item.id, posReport.starTime, posReport.endTime);
                if (balance != null)
                {
                    userclass.balance = balance.balance;
                }
                lstSource.Add(userclass);
            }
            this.dgvSysUserPayList.AutoGenerateColumns = false;
            this.dgvSysUserPayList.DataSource = lstSource;

            //设置统计绑定的表格
            uiDataGridViewFooter5.DataGridView = dgvSysUserPayList;
            uiDataGridViewFooter5["dataGridViewTextBoxColumn3"] = "合计：";
            uiDataGridViewFooter5["sumAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.sumAmount));
            uiDataGridViewFooter5["sumRealAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.sumRealAmount));
            uiDataGridViewFooter5["sumTaskAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.sumTaskAmount));
            uiDataGridViewFooter5["sumFeedAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.sumFeedAmount));
            uiDataGridViewFooter5["sumReduceAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.sumReduceAmount));
            uiDataGridViewFooter5["sumAllAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.sumAllAmount));
            uiDataGridViewFooter5["balance"] = CommFunc.ConvertToString(lstSource.Sum(p => p.balance));
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            userId.SelectedValue = 0;
            userId.Text = "";
            
        }
    }
}